package com.hqyj.mall.xiaomimall.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Console;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("commons")
public class CommonsController extends BaseController {

    @GetMapping("hutoolRandomCode")
    public void generateRandomCodeImgByHutool(HttpServletRequest request, HttpServletResponse response, String token) {
        try {
            //定义图形验证码的长和宽
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70,4,4);
            //重新生成验证码
            lineCaptcha.createCode();
            System.out.println("生成的验证码是：" + lineCaptcha.getCode());
            String randomCode = lineCaptcha.getCode();
            //保存到redis缓存中；
            this.redisUtil.set(token,randomCode,5); //在缓存中保存5分钟之后，自动过期了。
            lineCaptcha.write(response.getOutputStream());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
